package com.luyuan.gallery.service.impl;

import java.util.List;
import com.luyuan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.luyuan.gallery.mapper.ExhiCommentLikeMapper;
import com.luyuan.gallery.domain.ExhiCommentLike;
import com.luyuan.gallery.service.IExhiCommentLikeService;

/**
 * 评论点赞记录Service业务层处理
 * 
 * @author luyuan
 * @date 2025-11-10
 */
@Service
public class ExhiCommentLikeServiceImpl implements IExhiCommentLikeService {
    @Autowired
    private ExhiCommentLikeMapper exhiCommentLikeMapper;

    /**
     * 查询评论点赞记录
     * 
     * @param id 评论点赞记录主键
     * @return 评论点赞记录
     */
    @Override
    public ExhiCommentLike selectExhiCommentLikeById(Long id) {
        return exhiCommentLikeMapper.selectExhiCommentLikeById(id);
    }

    /**
     * 查询评论点赞记录列表
     * 
     * @param exhiCommentLike 评论点赞记录
     * @return 评论点赞记录
     */
    @Override
    public List<ExhiCommentLike> selectExhiCommentLikeList(ExhiCommentLike exhiCommentLike) {
        return exhiCommentLikeMapper.selectExhiCommentLikeList(exhiCommentLike);
    }

    /**
     * 新增评论点赞记录
     * 
     * @param exhiCommentLike 评论点赞记录
     * @return 结果
     */
    @Override
    public int insertExhiCommentLike(ExhiCommentLike exhiCommentLike) {
        exhiCommentLike.setCreateTime(DateUtils.getNowDate());
        return exhiCommentLikeMapper.insertExhiCommentLike(exhiCommentLike);
    }

    /**
     * 修改评论点赞记录
     * 
     * @param exhiCommentLike 评论点赞记录
     * @return 结果
     */
    @Override
    public int updateExhiCommentLike(ExhiCommentLike exhiCommentLike) {
        exhiCommentLike.setUpdateTime(DateUtils.getNowDate());
        return exhiCommentLikeMapper.updateExhiCommentLike(exhiCommentLike);
    }

    /**
     * 批量删除评论点赞记录
     * 
     * @param ids 需要删除的评论点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteExhiCommentLikeByIds(Long[] ids) {
        return exhiCommentLikeMapper.deleteExhiCommentLikeByIds(ids);
    }

    /**
     * 删除评论点赞记录信息
     * 
     * @param id 评论点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteExhiCommentLikeById(Long id) {
        return exhiCommentLikeMapper.deleteExhiCommentLikeById(id);
    }

    /**
     * 点赞或取消点赞评论
     * 
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int likeOrUnlikeComment(Long commentId, Long userId) {
        // 查询是否已有点赞记录
        ExhiCommentLike likeRecord = new ExhiCommentLike();
        likeRecord.setCommentId(commentId);
        likeRecord.setUserId(userId);
        List<ExhiCommentLike> existingLikes = exhiCommentLikeMapper.selectExhiCommentLikeList(likeRecord);
        
        if (existingLikes != null && !existingLikes.isEmpty()) {
            // 已有点赞记录，切换状态
            ExhiCommentLike existingLike = existingLikes.get(0);
            int newStatus = (existingLike.getStatus() == 1) ? 0 : 1;
            existingLike.setStatus(newStatus);
            return exhiCommentLikeMapper.updateExhiCommentLike(existingLike);
        } else {
            // 没有点赞记录，新增点赞
            ExhiCommentLike newLike = new ExhiCommentLike();
            newLike.setCommentId(commentId);
            newLike.setUserId(userId);
            newLike.setStatus(1); // 默认为点赞状态
            return exhiCommentLikeMapper.insertExhiCommentLike(newLike);
        }
    }
}
